How to Read a File in Python

您所在的位置:网站首页 read file How to Read a File in Python

How to Read a File in Python

2023-04-02 00:52| 来源: 网络整理| 查看: 265

You have seen various types of data holders before: integers, strings, lists. But so far, we have not discussed how to read or write files.

Related Course:Python Programming Bootcamp: Go from zero to hero

Read file

The Python programming language provides the ability to work with files using open().

Python programming treats some files as text files, where lines are separated by newline characters \n. You can open regular files with the paramater r.

Other files are considered binary and can be handled in a way that is similar to the C programming language. They need to be opened with the parameters rb.

read file into string

This is a sample program that shows how to read data from a file.

The file needs to be in the same directory as the program, if not you need to specify a path.

Create python script. Open editor of your choice and create new python script. Then paste the following code.

f = open("file.txt","r")lines = f.readlines()print(lines)

The read method readlines() reads all the contents of a file into a string.

Save the file with name example.py and run it.

read file line by line

To output line by line, you can use a for loop. The lines may include a new line character \n, that is why you can output using endl="".

f = open("filename.txt","r")lines = f.readlines()for line in lines: print(line, end="")

Another option is to remove the newline characters with the replace() method.

f = open("test.py","r")lines = f.readlines()for line in lines: line = line.replace("\n","") print(line) read file with keyword

The with keyword can be used to read files too. This automatically closes your file.

#!/usr/bin/env python# Define a filename.filename = "bestand.py"# Open the file as f.# The function readlines() reads the file.with open(filename) as f: content = f.readlines()# Show the file contents line by line.# We added the comma to print single newlines and not double newlines.# This is because the lines contain the newline character '\n'.for line in content: print(line),

The first part of the code will read the file content. All of the lines read will be stored in the variable content. The second part will iterate over every line in the variable contents.

If you do not want to read the newline characters ‘\n’, you can change the statement f.readlines() to this:

content = f.read().splitlines()

Resulting in this code:

#!/usr/bin/env python# Define a filename.filename = "bestand.py"# Open the file as f.# The function readlines() reads the file.with open(filename) as f: content = f.read().splitlines()# Show the file contents line by line.# We added the comma to print single newlines and not double newlines.# This is because the lines contain the newline character '\n'.for line in content:print(line)

While the codes above work, we should always test if the file we want to open exists.  We will test first if the file does not exist, if it does it will read the file else return an error. As in the code below:

#!/usr/bin/env pythonimport os.path# Define a filename.filename = "bestand.py"if not os.path.isfile(filename): print('File does not exist.')else:# Open the file as f.# The function readlines() reads the file.with open(filename) as f: content = f.read().splitlines()# Show the file contents line by line.# We added the comma to print single newlines and not double newlines.# This is because the lines contain the newline character '\n'.for line in content: print(line)

If you are new to Python programming, I highly recommend this book.

Download Python Exercises

BackNext


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3